/**
 * 代理商管理  代理商配置
 */
<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>代理商配置</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索筛选 -->
    <el-form :inline="true" :model="formInline" class="user-search">
      <el-form-item label="搜索：">
        <el-input size="small" v-model="formInline.deptName" placeholder="输入代理商"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button size="small" type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        <el-button size="small" type="primary" icon="el-icon-plus" @click="openModi">添加</el-button>
      </el-form-item>
      <el-form-item>
        <el-select size="small" v-model="formInline.state" placeholder="请选择" @change="selectChange">
        <el-option v-for="item in payType" :label="item.key" :value="item.value" :key="item.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <!--列表-->
    <el-table size="small" :data="listData" highlight-current-row v-loading="loading" border element-loading-text="拼命加载中" style="width: 100%;">
      <el-table-column align="center" type="selection" width="60">
      </el-table-column>
      <el-table-column sortable prop="nickname" label="昵称" width="150">
      </el-table-column>
      <el-table-column sortable prop="name" label="名字" width="150">
      </el-table-column>
      <el-table-column sortable prop="phone" label="手机号" width="150">
      </el-table-column>
      <el-table-column sortable prop="balance" label="账户余额" width="150">
      </el-table-column>
      <el-table-column sortable prop="dividedInto" label="分成比例" width="200">
        <template slot-scope="scope" >
          <el-col :span="12"><div>{{scope.row.dividedInto}}</div></el-col> 
			    <el-button size="mini" type="primary" @click="openDialog(scope.$index, scope.row)">修改</el-button>
        </template>
      </el-table-column>
      <el-table-column  label="操作" min-width="300">
        <template slot-scope="scope">
			    <el-button size="mini" type="danger" @click="handleEdit(scope.$index, scope.row,'封禁')" v-if="scope.row.state==0">封禁</el-button>
          <el-button size="mini" type="warning" @click="handleEdit(scope.$index, scope.row,'解封')" v-if="scope.row.state==1">解封</el-button>
          <el-button size="mini" type="primary" @click="handleEdit(scope.$index, scope.row,'同意注销')" v-if="scope.row.state==2">同意注销</el-button>
		      <el-button size="mini" type="primary" v-if="scope.row.state==2" @click="handleEdit(scope.$index, scope.row,'拒绝注销')">拒绝注销</el-button>
		      <el-button size="mini" type="info" v-if="scope.row.state==3" @click="handleEdit(scope.$index, scope.row,'取消注销')">取消注销</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页组件 -->
    <Pagination v-bind:child-msg="pageparm" @callFather="callFather"></Pagination>
    <!-- 修改分成比例界面 -->
		<el-dialog class="poup" title="修改分成比例" :visible.sync="editFormVisible" width="30%" @click="closeDialog">
			<el-form label-width="120px" :model="cardObj" :rules="rules" ref="cardObj">
				<el-form-item label="分成比例" prop="dividedInto">
          <el-input size="small" type="number" v-model="cardObj.dividedInto" auto-complete="off" placeholder="请输入分成比例"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeDialog">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="updateRatio">保存</el-button>
			</div>
		</el-dialog>
    <!-- 新增代理商界面 -->
		<el-dialog class="poup" title="新增代理商" :visible.sync="addFormModi" width="30%" @click="closeModi">
			<el-form label-width="120px" :model="modiObj" :rules="modirules" ref="modiObj">
				<el-form-item label="帐号" prop="account">
          <el-input size="small" v-model="modiObj.account" auto-complete="off" placeholder="请输入帐号"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input size="small" v-model="modiObj.password" auto-complete="off" placeholder="请输入密码"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="phone">
          <el-input size="small" v-model="modiObj.phone" auto-complete="off" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input size="small" v-model="modiObj.name" auto-complete="off" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="身份证" prop="IdCard">
          <el-input size="small" v-model="modiObj.IdCard" auto-complete="off" placeholder="请输入身份证"></el-input>
        </el-form-item>
        <el-form-item label="分成比" prop="dividedInto">
          <el-input size="small" type="number" v-model="modiObj.dividedInto" auto-complete="off" placeholder="请输入分成比例"></el-input>
        </el-form-item>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button size="small" @click="closeModi">取消</el-button>
				<el-button size="small" type="primary" :loading="loading" class="title" @click="addModi">保存</el-button>
			</div>
		</el-dialog>
  </div>
</template>

<script>
import { stratorList, addStrator, upStratorSta, upStratorRatio } from '../../api/userMG'
import Pagination from '../../components/Pagination'
export default {
  data() {
    return {
      editFormVisible: false, //控制修改分成比例页面显示与隐藏
      addFormModi: false, //控制新增代理商显示与隐藏
      loading: false, //是显示加载
      formInline: {
        page: 1,
        pagenum: 10,
		    //state:null,
      },
      cardObj:{
        adminstratorID: '',
        dividedInto: ''
      },//修改分成比例输入信息
      modiObj:{
        account: '',
        password: '',
        phone: '',
        name: '',
        IdCard: '',
        dividedInto: ''
      },//新增代理商输入信息
      // rules表单验证
			rules: {
				dividedInto: [{ required: true, message: '请输入分成比例', trigger: 'blur' }]
			},
      modirules: {
				account: [{ required: true, message: '请输入帐号', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        phone: [{ required: true, message: '请输入手机号', trigger: 'blur' }],
        name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
        IdCard: [{ required: true, message: '请输入身份证', trigger: 'blur' }],
        dividedInto: [{ required: true, message: '请输入分成比例', trigger: 'blur' }]
			},
      listData: [], //用户数据
      payType: [
        { key: '请选择', value: '' },
        { key: '正常代理', value: 0 },
        { key: '已封禁', value: 1 },
        { key: '申请注销', value: 2 },
        { key: '已注销', value: 3 },
      ],
      // 分页参数
      pageparm: {
        currentPage: 1,
        pageSize: 10,
        total: 10
      }
    }
  },
  // 注册组件
  components: {
    Pagination
  },
  /**
   * 数据发生改变
   */

  /**
   * 创建完毕
   */
  created() {
    this.getdata(this.formInline)
  },

  /**
   * 里面的方法只有被调用才会执行
   */
  methods: {
    //选择器改变事件
    selectChange(e){
      if(this.formInline.state!==0&&!this.formInline.state){
        delete this.formInline.state;
      }
      this.getdata(this.formInline)
    },
    // 获取列表
    getdata(parameter) {
      this.loading = true
      stratorList(parameter).then(res => {
        this.loading = false
        this.$message({
          type: 'info',
          message: res.message
        })
        this.listData = res.date;
        // 分页赋值
        // this.pageparm.currentPage = this.formInline.page
        // this.pageparm.pageSize = this.formInline.limit
        this.pageparm.total = res.count
      }).catch(err => {
        this.loading = false
        this.$message.error('菜单加载失败，请稍后再试！')
      })
    },
    // 分页插件事件
    callFather(parm) {
      this.formInline.page = parm.currentPage
      this.formInline.pagenum = parm.pageSize
      this.getdata(this.formInline)
    },
    // 搜索事件
    search() {
      this.getdata(this.formInline)
    },
    // 关闭新增代理商弹出框
		closeModi() {
			this.addFormModi = false;
		},
    //显示新增代理商弹出框
		openModi() {
			this.addFormModi = true;
		},
    // 新增代理商函数
    addModi(){
      addStrator(this.modiObj).then(res => {
        this.closeModi()
        this.getdata(this.formInline)
      }).catch(err => {
        this.closeModi()
        this.loading = false
      })
    },
    // 关闭分成比例弹出框
		closeDialog() {
			this.editFormVisible = false;
		},
    //显示分成比例弹出框
		openDialog: function(index,row) {
      this.cardObj.adminstratorID=row.ID;
      this.cardObj.dividedInto=row.dividedInto;
			this.editFormVisible = true;
		},
    // 修改代理商分成比例
    updateRatio(){
      upStratorRatio(this.cardObj).then(res => {
        this.closeDialog()
        this.getdata(this.formInline)
      }).catch(err => {
        this.closeDialog()
        this.loading = false
      })
    },
    // 修改代理商状态
    handleEdit: function(index, row, type) {
      this.$confirm('确定要'+type+'吗?', '信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let parameter={state:row.state,adminstratorID:row.ID};
        if(row.state==0){
          parameter.state=1;
        }else if(row.state==1){
          parameter.state=0;
        }else if(row.state==2){
          if(type=='同意注销'){
            parameter.state=3;
          }else if(type=='拒绝注销'){
            parameter.state=0;
          }
        }else if(row.state==3){
          parameter.state=0;
        }
        upStratorSta(parameter).then(res => {
          this.getdata(this.formInline)
        }).catch(err => {
          this.loading = false
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'+type
        })
      })
    },
  }
}
</script>

<style scoped lang="less">
.user-search {
  margin-top: 20px;
}
.userRole {
  width: 100%;
}
.avatar {
	width: 80px;
	height: 80px;
	display: block;
}
.avatar-uploader {
	.el-upload {
		border: 1px dashed #8c939d;
		border-radius: 6px;
		cursor: pointer;
		position: relative;
		overflow: hidden;
	}
	.el-upload:hover {
		border-color: #409eff;
	}
}
.avatar-uploader-icon {
	border: 1px dashed #8c939d;
	font-size: 28px;
	color: #8c939d;
	width: 80px;
	height: 80px;
	line-height: 80px;
	text-align: center;
}
</style>

 